//
//  TeacherHomeVC.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/6.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "TeacherHomeVC.h"
#import "HNTeacherAttenceVC.h" // 老师考勤
#import "HMTeacherViewController.h" // 绩效详情
#import "HNGiveAwayFlowerVCViewController.h" // 赠送红花
#import "ZYHistoricalRecordController.h" // 评价记录
#import "TeacherCourseListVC.h" // 课程列表/评价
#import "ZYMiyoshiBankController.h" // 三好银行
#import "ZYNewsMessageController.h" // 消息
#import "ZYHomeMemanagerController.h"
@interface TeacherHomeVC ()
{
    __weak IBOutlet UIImageView *_Img; // 头像
    __weak IBOutlet UILabel *_name; // 名称
    __weak IBOutlet UILabel *_Phone; // 电话
    
    UIView *_MessageRedBtn; // 消息红点按钮
    UIButton *_messageBtn; // 消息按钮
    __weak IBOutlet UILabel *strCoinsLabel;
}
@end

@implementation TeacherHomeVC
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];

    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(184, 50, 59)] forBarMetrics:(UIBarMetricsDefault)];

    // 检查小红点
    [self setNetTowWork];
    [self setNetWorkCoins];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [UIBarButtonItem itemWithTargat:nil action:nil image:@"" highImage:@""];
    // 设置导航栏按钮
    [self SetMessageButton];

    _name.text = [AccountTool shareAccountTool].currentAccount.petName;
    _Phone.text = [AccountTool shareAccountTool].currentAccount.username;
    [[AccountTool shareAccountTool].currentAccount.avatar isKindOfClass:[NSNull class]] ? nil:[_Img sd_setImageWithURL:[NSURL URLWithString:[AccountTool shareAccountTool].currentAccount.avatar] placeholderImage:[UIImage imageNamed:@"bg_home_head_default_new"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        // 变圆
        _Img.layer.cornerRadius = _Img.frame.size.width / 2;
        _Img.layer.masksToBounds = YES;
    }];
}






- (void)setNetWorkCoins{
    
    NSString *url = [NSString stringWithFormat:@"user.do?action=getMyInfoAndRecharge"];
    
    NSLog(@"%@  %@",LLBaseURLs(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            strCoinsLabel.text = [NSString stringWithFormat:@"%ld星币",[[t valueForKey:@"money"]  integerValue]];
            
        }
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
    
    
    
}















#pragma mark - 网络
/** 检查小红点 */
- (void)setNetTowWork{

    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=countCommentReply"];
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            NSString *t = [NSString stringWithFormat:@"%@",[Obj valueForKey:@"t"]];
            if ([t integerValue] == 0) {
                _messageBtn.hidden = _MessageRedBtn.hidden = YES;
            }else{
                _messageBtn.hidden = _MessageRedBtn.hidden = NO;
            }
        }
    } Fail:^(NSError *Error) {
    }];
}



#pragma mark - 自定义方法
/** 设置消息按钮 */
- (void)SetMessageButton {
    
    UIButton *Button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [Button setBackgroundImage:[UIImage imageNamed:@"icon_message_default_new_normal"] forState:(UIControlStateNormal)];
    [Button setBackgroundImage:[UIImage imageNamed:@"icon_message_default_new_normal"] forState:(UIControlStateHighlighted)];
    [Button addTarget:self action:@selector(allButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    Button.frame = CGRectMake(0, 0, 25, 16);
    _messageBtn = Button;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Button];
    
    // 红点
    _MessageRedBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _MessageRedBtn.frame = CGRectMake(Button.frame.size.width - 4, -4, 8, 8);
    _MessageRedBtn.backgroundColor = [UIColor redColor];
    _MessageRedBtn.layer.masksToBounds = YES;
    _MessageRedBtn.layer.cornerRadius = _MessageRedBtn.frame.size.width/2;
    _MessageRedBtn.hidden = YES;
    [Button addSubview:_MessageRedBtn];
}



#pragma mark - 点击事件
/** 本页按钮点击事件 */
- (IBAction)ClickAction:(UIButton *)sender {
    
    // 课程评价
    if (sender.tag == 1100) {
        
        [self.navigationController pushViewController:[[TeacherCourseListVC alloc] init] animated:YES];
    }
    
    // 绩效详情
    if (sender.tag == 1101) {
        
        [self.navigationController pushViewController:[[HMTeacherViewController alloc] init] animated:YES];
    }
    
    // 老师考勤
    if (sender.tag == 1102) {
     
        [self.navigationController pushViewController:[HNTeacherAttenceVC new] animated:YES];
    }
    
    // 三好银行
    if (sender.tag == 1103) {
       
        [self.navigationController pushViewController:[ZYMiyoshiBankController  new] animated:YES];
    }
    
    // 评价记录
    if (sender.tag == 1104) {
        
        [self.navigationController pushViewController:[ZYHistoricalRecordController new] animated:YES];
    }
    
    // 赠送红花
    if (sender.tag == 1105) {
        
        [self.navigationController pushViewController:[HNGiveAwayFlowerVCViewController new] animated:YES];
    }
    // 作业批注
    if (sender.tag == 1106) {
        ZYHomeMemanagerController *vc = [ZYHomeMemanagerController new];
        vc.isTeacher = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

/** 右上角的消息点点击事件 */
- (void)allButtonAction {
    
    ZYNewsMessageController *vc = [ZYNewsMessageController new];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
@end
